package com.autocar.reader.view;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.autocar.reader.R;
import com.autocar.reader.bean.VersionBean;
import com.autocar.reader.util.ViewUtil;

public class VersionView {

	private View rootView;
	private TextView titleTv;
	private TextView contentTv;
	private Button noBtn;
	private Button yesBtn;
	private PopupWindow versionDialog;
	private Context mContext;

	public VersionView(Context context) {
		this.mContext = context;
		rootView = LayoutInflater.from(context).inflate(
				R.layout.version_layout, null);
		titleTv = (TextView) rootView.findViewById(R.id.version_tv_title);
		contentTv = (TextView) rootView.findViewById(R.id.version_tv_content);
		noBtn = (Button) rootView.findViewById(R.id.version_btn_no);
		yesBtn = (Button) rootView.findViewById(R.id.version_btn_yes);

		versionDialog = ViewUtil.getPopupWindow(rootView, false);
	}

	public View getView(String content, final String yesUrl) {

//		titleTv.setText(title);
		contentTv.setText(content);
//		noBtn.setText(noValue);
//		yesBtn.setText(yesValue);

		noBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (versionDialog != null) {
					versionDialog.dismiss();
				}
			}

		});

		yesBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (versionDialog != null && yesUrl != null && !"".equals(yesUrl)) {
					gotoWebActivity(yesUrl);
				}
				versionDialog.dismiss();
			}
		});

		return rootView;
	}
	
	private void gotoWebActivity(String url) {
		Intent viewIntent = new Intent("android.intent.action.VIEW");
		Uri uri = Uri.parse(url);
		viewIntent.setDataAndType(uri, "text/html");
		mContext.startActivity(viewIntent);
	}

//	public Dialog getDialog(String title, String content, String noValue,
//			String yesValue, final String yesUrl) {
//		View versionView = getView(title, content, noValue, yesValue, yesUrl);
//		versionDialog.setContentView(versionView);
//		return versionDialog;
//	}

	public PopupWindow getDialog(VersionBean vb) {
			if(vb == null)
				return null;
			View versionView = getView(vb.getUpdate_detail(),vb.getApp_url());
			versionDialog.setContentView(versionView);
			return versionDialog;
	}
}
